package com.training.admin.web.controller.api;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.training.admin.service.CourseTraineeStatService;

@RestController
@RequestMapping("/api")
public class ApiController {
    
    private static final Logger logger = LoggerFactory.getLogger(ApiController.class);
    
    @Autowired
    private CourseTraineeStatService courseTraineeStatService;
    
    @RequestMapping(value = "/course-trainee-stats", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody HttpResult listCourseTraineeStats(@RequestBody String body) {
        
        HttpResult httpResult = new HttpResult();
        
        Map<String, Object> requestParams = JsonUtils.parse(body);
        String email = JsonUtils.parse(JsonUtils.toJson(requestParams.get("email")), String.class);
        List<Long> courseIds = JsonUtils.parseAsList(JsonUtils.toJson(requestParams.get("courseIds")), Long.class);
        
        try{
            
            Map<String, Boolean> result = courseTraineeStatService.listCourseTraineeStats(email, courseIds);
            httpResult.success().data("courseTraineeStatMaps", result).message("list course trainee stat success");
            
        } catch (Exception e) {
            logger.error("#listCourseTraineeStats error - ", e);
            httpResult.fail("500").message("list course trainee stat failed");
            
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param body
     * @return
     */
    @RequestMapping(value = "/feiren-status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult listProjectStatus(@RequestBody String body) {
        
        HttpResult httpResult = new HttpResult();
        
        Map<String, Object> requestParams = JsonUtils.parse(body);
        String email = JsonUtils.parse(JsonUtils.toJson(requestParams.get("email")), String.class);
        
        try {
            
            httpResult = courseTraineeStatService.finishedFeiRen(email);
            
        } catch (Exception e) {
            
            logger.error("#listCourseTraineeStats error - ", e);
            httpResult.fail("500").message("list course trainee stat failed");
            
        }
        
        return httpResult;
    }

}
